#!/usr/bin/env python3
import os
import glob
import shutil

def copy_result_files():
    """
    复制所有outputs_x目录下的result.txt文件并重命名为result_x.txt
    """
    # 查找所有outputs_*目录
    output_dirs = glob.glob("outputs_*")
    
    # 按数字排序
    # output_dirs.sort(key=lambda x: int(x.split('_')[1]))
    
    print(f"找到 {len(output_dirs)} 个outputs目录")
    
    copied_count = 0
    for output_dir in output_dirs:
        if output_dir == "outputs_bs_16" or output_dir == "outputs_bs_64":
            continue
        # 提取目录编号
        dir_num = output_dir.split('_')[1]
        
        # 源文件路径
        source_file = os.path.join(output_dir, "result.txt")
        
        # 目标文件路径
        target_file = f"results/result_{dir_num}.txt"
        
        # 检查源文件是否存在
        if os.path.exists(source_file):
            try:
                # 复制文件
                shutil.copy2(source_file, target_file)
                print(f"已复制: {source_file} -> {target_file}")
                copied_count += 1
            except Exception as e:
                print(f"复制失败 {source_file}: {e}")
        else:
            print(f"文件不存在: {source_file}")
    
    print(f"\n总共复制了 {copied_count} 个文件")

if __name__ == "__main__":
    copy_result_files() 